package com.bci.pwtz.service.impl;

import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.ResourceMapper;
import com.bci.pwtz.mysql.model.Resource;
import com.bci.pwtz.service.ResourceService;

public class ResourceServiceImpl implements ResourceService{
   private ResourceMapper objMapper;
   
	public ResourceMapper getObjMapper() {
	return objMapper;
}

  public void setObjMapper(ResourceMapper objMapper) {
	this.objMapper = objMapper;
}
  private static final Logger _logger=Logger.getLogger(ResourceServiceImpl.class);
	@Override
	public int add(Resource res) throws PwtzDatabaseException {
		int result =0 ;
		if(_logger.isDebugEnabled()){
			_logger.debug("add:Resource="+res);
		}
		if(res==null){
			if(_logger.isDebugEnabled()){
				_logger.debug("add,Resource is null");
			}
		return result;
		}
		try {
			res.setStatus(1);
			objMapper.insert(res);
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public int delete(Long resid) throws PwtzDatabaseException
			 {
		
		int result = 0;
		try{
		objMapper.deleteById(resid);
		result = 1;
		}
		 catch (DataAccessException e)
	        {
	            _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
	        }
		return result;
	}

	@Override
	public int update(Resource resource) throws PwtzDatabaseException {
		int result =0 ;
		try {
			objMapper.updateByPrimaryKey(resource);
			result = 1;
		} catch (Exception e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return result;
	}

	@Override
	public Resource load(Long resid) throws PwtzDatabaseException {
		Resource res = null;
		try{
		res = objMapper.selectByPrimaryKey(resid);
		}
		catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (res != null)
        {
            _logger.debug("load:result dysydlbhq=" + res);
        }
		return res;
	}

	@Override
	public PaginationList<Resource> findResource(int pageSize, int pageNo,int restype,String resname)
			throws PwtzDatabaseException {
		 PaginationList<Resource> paginationList =new PaginationList<Resource>();
		 List<Resource> list=null;
		 int beginIndex = pageSize * pageNo - pageSize;
		 try{
		 list = objMapper.selectResourceList(beginIndex, pageSize,restype,resname);
		 }
		 catch (DataAccessException e)
	        {
	            _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
	        }
		 
		    int totalCount = 0;
	        int totalPage =1;
	        if(pageSize!=0){
	            try
	            {
	                totalCount = objMapper.selectResourceCount(restype,resname);
	            }
	            catch (DataAccessException e)
	            {
	                _logger.error(ExceptionUtils.getStackTrace(e));
	                throw new PwtzDatabaseException();
	            }
	            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
	        }else{
	            pageNo=1;
	            if(list!=null && list.size()>0){
	                totalCount = list.size();
	            }
	        }
	        paginationList.setTotalPage(totalPage);
	        paginationList.setCurrPage(pageNo);
	        paginationList.setPageSize(pageSize);
	        paginationList.setRecords(list);
	        paginationList.setTotalRecord(totalCount);
	        return paginationList;
	}

}
